package ch.zbw.sysventory.server.scanner;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ch.zbw.sysventory.server.model.ComputerScan;
import ch.zbw.sysventory.server.model.User;
import ch.zbw.sysventory.server.service.ComputerScanService;
import ch.zbw.sysventory.server.service.ScanConfigurationService;

@Component
public class ScanManager {

	@Autowired
	private ScanConfigurationService scanConfigurationService;
	@Autowired
	private ComputerScanner computerScanner;
	@Autowired
	private ComputerScanService computerScanService;
	private NetworkScanJob scanJob;
	private List<String> logMessages;

	public void startScan(User user) {
		this.logMessages = new ArrayList<String>();
		if (this.scanJob == null) {
			ComputerScan computerScan = new ComputerScan();
			computerScan.setStartTimeStamp(new Date());
			computerScan.setStartUser(user);
			computerScan = this.computerScanService.save(computerScan);
			this.scanJob = new NetworkScanJob(this, this.scanConfigurationService.getConfiguration(), this.computerScanner, computerScan);
			scanJob.start();
		}
	}

	public void setScanJobFinished() {
		this.scanJob = null;
	}

	public boolean isJobRunning() {
		return this.scanJob != null;
	}

	public void log(String message) {
		this.logMessages.add(message);
	}

	public List<String> getLogMessages() {
		return this.logMessages;
	}

}
